package com.anlu.base.reflection;

import lombok.Data;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.ResolvableType;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class RefTest1 {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());


    @Test
    void test1(){

        List<Student1> list = new ArrayList<Student1>();
        list.add(new Student1());


        ResolvableType resolvableType = ResolvableType.forInstance(list);
        Class<?> genericType = resolvableType.getGeneric(0).resolve();
        logger.info("genericType:{}",genericType.getName());

//        getValue(list.getClass(),list);

        getListGenericType(list);

//        getValue(Student1.class,new Student1());
    }


    private void getValue(Class T,Object obj){

        Class<?> clazz = obj.getClass();
        Type genericType = clazz.getGenericSuperclass();
        Type type = T.getGenericSuperclass();

        if (T.isAssignableFrom(Student1.class)) {
            logger.info("是学生");
        }

        //检查List.class 是否是clazz的父类 不考虑泛型
        if(List.class.isAssignableFrom(clazz)){
          logger.info("是List的子类");
        }

        if(genericType instanceof ParameterizedType){
            ParameterizedType pt = (ParameterizedType) genericType;
            if(List.class.isAssignableFrom((Class<?>) pt.getRawType()) ){
                System.out.println("这是一个 List 泛型类型");
                Type[] typeArgs = pt.getActualTypeArguments();
                System.out.println("泛型参数类型: " + typeArgs[0]);

                Class<?> cla = (Class<?> )typeArgs[0];
                if(cla.isAssignableFrom(Student1.class)){
                    logger.info("student1");
                }

            }
        }

    }

    @Data
    class Student1{
        private String name;
    }

    private void getListGenericType(List<?> list) {
        try {
            // 1. 获取List的实际运行时Class对象
            Class<?> clazz = list.getClass();

            // 2. 获取List的泛型类型（通过getGenericSuperclass()无法获取，需要其他方式）
            // 注意：直接通过list.getClass()无法获取泛型参数，需要通过字段或方法签名

            // 3. 替代方案：通过匿名子类获取（适用于测试场景）
            List<Student1> listWithGeneric = new ArrayList<Student1>() {};
            Type genericSuperclass = listWithGeneric.getClass().getGenericSuperclass();

            if (genericSuperclass instanceof ParameterizedType) {
                ParameterizedType pt = (ParameterizedType) genericSuperclass;
                Type[] actualTypeArguments = pt.getActualTypeArguments();

                if (actualTypeArguments.length > 0) {
                    Type typeArg = actualTypeArguments[0];
                    if (typeArg instanceof Class) {
                        Class<?> genericClass = (Class<?>) typeArg;
                        System.out.println("List的泛型类型: " + genericClass.getName());

                        if (genericClass.isAssignableFrom(Student1.class)) {
                            System.out.println("泛型是Student1或其子类");
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}


